import os
import fnmatch
from colorizer import colorizer
import doxygen

env = Environment(CXXFLAGS=Split("-Isrc -std=c++0x -Wno-write-strings -g"),
                  LINKFLAGS=["-g"],
                  CXXDEFINES=[],
                  LIBS=Split("m fltk fltk_cairo"),
                  CPPPATH='src')
env.Decider("MD5")
col = colorizer()
col.colorize(env)
doxygen.generate(env)
env.Doxygen('doc/Doxyfile')

v = Variables('build.conf')
v.Add('target', '', 'release')
v.Add('usesOpenmp', '', True)
v.Add('fltk', '', '')
v.Update(env)
if 'configure' in COMMAND_LINE_TARGETS:
    conf = Configure(env)

    AddOption("--target",
              dest="target",
              type="string",
              nargs=1,
              action="store",
              metavar="BUILD_TARGET",
              help="build type")
    AddOption("--disable-openmp",
              nargs=0,
              dest="openmp_disable",
              help="build without using openmp")
    AddOption("--enable-openmp",
              nargs=0,
              dest="openmp_enable",
              help="build using openmp")
    AddOption("--fltk",
              nargs=1,
              dest="fltk",
              type="string",
              action="store",
              help="fltk prefix")
    headers_core = env.Split("stdlib.h math.h string.h signal.h")
    headersCXX_core = env.Split("iostream vector memory sstream fstream")
    libs_core = env.Split("")

    env['target'] = GetOption("target")
    if env['target'] == None: env['target'] = "release"
    if env['target']=="release":
        print "Build type: release"
        env['LINKFLAGS'] += env.Split("-flto")
        env['CXXFLAGS'] += env.Split("-flto -O3")
        env['usesOpenmp'] = True
    if env['target']=="debug":
        print "Build type: debug"
        env['LINKFLAGS'] += env.Split("-g")
        env['CXXFLAGS'] += env.Split("-g")
        env['usesOpenmp'] = False
    if GetOption("openmp_disable")!=None:
        env['usesOpenmp'] = False
    if GetOption("openmp_enable")!=None:
        env['usesOpenmp'] = True

    env['fltk'] = GetOption('fltk')
    if env['fltk'] == None: env['fltk'] = ''
    
    for h in headers_core:
        if not conf.CheckHeader(h):
            print 'C header "'+h+'" is needed to compile"'
            Exit(0)
    for h in headersCXX_core:
        if not conf.CheckCXXHeader(h):
            print 'C++ header "'+h+'" is needed to compile'
            Exit(0)
    for l in libs_core:
        if not conf.CheckLib('lib'+l):
            print 'Library lib'+l+' is needed to link program'
            Exit(0)
    
    env = conf.Finish()

    v.Save('build.conf', env)
else:
    if env['target']=="release":
        env['LINKFLAGS'] += env.Split("-flto")
        env['CXXFLAGS'] += env.Split("-flto -O3")
    if env['target']=="debug":
        env['LINKFLAGS'] += env.Split("-g")
        env['CXXFLAGS'] += env.Split("-g")

if env['usesOpenmp']:
    env['CXXDEFINES'].append("USE_OPENMP")
    env['CXXFLAGS'].append("-fopenmp")
    env['LINKFLAGS'].append("-fopenmp")

env['CXXFLAGS'].append("-I/usr/include/"+env['fltk'])
env['LINKFLAGS'].append("-L/usr/lib/"+env['fltk'])
for D in env['CXXDEFINES']:
    env['CXXFLAGS'].append("-D"+D)
    
#print summary
print "BUILD="+env['target']
print "OPENMP="+str(env['usesOpenmp'])
print "FLTK="+env['fltk']

import fnmatch
def find_files(path,pattern):
    matches = []
    for root, dirs, files in os.walk(path):
        for filename in fnmatch.filter(files, pattern):
            matches.append(os.path.join(root, filename))
    return matches

sources = find_files('src','*.cpp')
objs = []
for s in sources:
    objs.append(env.Object(s))

prender = env.Program("bin/prender", objs)
env.AlwaysBuild(prender)
